//
// Created by hkkzzxz on 24-8-31.
//

#ifndef YUNSHI_MYSQLCONNECTIONPOOL_H
#define YUNSHI_MYSQLCONNECTIONPOOL_H

#include <iostream>
#include <memory>
#include <queue>
#include <condition_variable>
#include <mysql/jdbc.h>
#include "LogManager.h"
#include "Singleton.h"

class MySQLConnectionPool : public Singleton<MySQLConnectionPool> {
    friend class Singleton<MySQLConnectionPool>;

public:
    //从连接池获取连接
    std::shared_ptr<sql::Connection> getConnection();

    //将连接释放回连接池
    void releaseConnection(std::shared_ptr<sql::Connection> conn);

private:
    MySQLConnectionPool();

    ~MySQLConnectionPool();

    // 规定连接池的最大连接数量
    const int maxPoolSize = 10;

    const std::string MySQLAddress = "tcp://127.0.0.1:3306/yun_shi";
    const std::string MySQLAccount = "root";
    const std::string MySQLPassword = "root";

    // 创建一个新的数据库连接
    std::shared_ptr<sql::Connection> createConnection();

    // 储存数据库的连接队列
    std::queue<std::shared_ptr<sql::Connection>> pool_;
    // 用于同步访问连接池的互斥锁
    std::mutex mutex_;
    // 用于线程间同步的条件变量
    std::condition_variable cv_;

    LogManager *logManager = LogManager::getInstance();
    std::shared_ptr<spdlog::logger> consoleLogger = logManager->getConsoleLogger();
    std::shared_ptr<spdlog::logger> fileLogger = logManager->getFileLogger();

};


#endif //YUNSHI_MYSQLCONNECTIONPOOL_H
